﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using Billing.Utilities;
using System.Data;
using Billing.Controls;

namespace Billing.Windows
{
    /// <summary>
    /// Interaction logic for AddLeave.xaml
    /// </summary>
    public partial class AddOT : Window
    {
        public bool IsExit { get; set; }
        List<EmployeeItemControl> mainList = new List<EmployeeItemControl>();
        public AddOT()
        {
            InitializeComponent();

            try
            {
                lstBranch.ItemsSource = Database.GetBranches();
                lstBranch.DisplayMemberPath = "BranchName";
                lstBranch.SelectedValuePath = "BranchID";
                lstBranch.SelectedIndex = 0;
                lstBranch.Focus();

                //lstEmpName.ItemsSource = Database.GetEmployees();
                //lstEmpName.DisplayMemberPath = "EmpName";
                //lstEmpName.Focus();
                //lstEmpName.SelectedIndex = 0;

                mainList.Add(new EmployeeItemControl());
                foreach (DataRow row in Database.GetCreditTransactionsAddedToday(2).Rows)
                {
                    EmployeeItemControl item = new EmployeeItemControl();
                    item.lblEmp.Content = row[0].ToString();
                    item.lblDate.Content = DateTime.Parse(row[1].ToString()).ToString("dd/MMM/yyyy");
                    item.lblIsHalfDay.Content = (row[2].ToString() == "False") ? "Full" : "Half";
                    mainList.Add(item);
                }

                lbOTGrid.ItemsSource = mainList;
            }
            catch (Exception) { }
        }

        private void date_PreviewKeyDown(object sender, KeyEventArgs e)
        {
            try
            {
                if (e.Key == Key.Enter)
                {
                    if (date.IsFocusedOnDate)
                        date.SetFocusOnMonth();
                    else if (date.IsFocusedOnMonth)
                        date.SetFocusOnYear();
                    else if (date.IsFocusedOnYear)
                        date.SetFocusOnHours();
                    else if (date.IsFocusedOnHours)
                        IsHalfDay.Focus();
                    else if (IsHalfDay.IsFocused)
                        bAddStock.Background = new SolidColorBrush(Colors.White);
                }
                else if (e.Key == Key.Right && date.IsFocusedOnDate)
                    date.SetFocusOnMonth();
                else if (e.Key == Key.Left && date.IsFocusedOnDate)
                {
                    TraversalRequest request = new TraversalRequest(FocusNavigationDirection.Previous);
                    UIElement elementWithFocus = Keyboard.FocusedElement as UIElement;
                    if (elementWithFocus != null)
                        elementWithFocus.MoveFocus(request);
                    //lstEmpName.Focus();
                }
            }
            catch (Exception) { }
        }

        private void Window_KeyUp(object sender, KeyEventArgs e)
        {
            try
            {
                if (e.Key == Key.Escape)
                {
                    this.IsExit = true;
                    this.Close();
                }
                else if (e.Key == Key.F2)
                {
                    IsHalfDay.IsChecked = !IsHalfDay.IsChecked;
                }
                else if (e.Key == Key.F1)
                {
                    if (MessageBoxResult.OK == MessageBox.Show("Do you want to Add new OT?", "Confirm Add OT", MessageBoxButton.OKCancel))
                    {
                        int name = ((Billing.Utilities.Employee)(lstEmpName.SelectedItem)).EmpID;
                        DateTime leaveDate = new DateTime(date.nudYear.Value, date.nudMonth.Value, date.nudDate.Value);
                        bool isHalfDay = IsHalfDay.IsChecked.Value;

                        int result = Database.AddLeaveOrOT(name.ToString(), LeaveOrOT.OT, leaveDate.ToString("MM/dd/yyyy"), isHalfDay);
                        if (result == -2)
                        {
                            MessageBox.Show("Leave/O.T already added for this employee on this date.", "OT Already Added", MessageBoxButton.OK);
                            return;
                        }
                        else if (result < 0)
                        {
                            MessageBox.Show("Some unknown error has occurred.", "Unknown Error.", MessageBoxButton.OK);
                            return;
                        }
                        else
                        {
                            MessageBox.Show("O.T Added Successfully.                        ", "Success", MessageBoxButton.OK);

                            mainList.Clear();
                            lbOTGrid.ItemsSource = null;
                            mainList.Add(new EmployeeItemControl());
                            foreach (DataRow row in Database.GetCreditTransactionsAddedToday(2).Rows)
                            {
                                EmployeeItemControl item = new EmployeeItemControl();
                                item.lblEmp.Content = row[0].ToString();
                                item.lblDate.Content = DateTime.Parse(row[1].ToString()).ToString("dd/MMM/yyyy");
                                item.lblIsHalfDay.Content = (row[2].ToString() == "False") ? "Full" : "Half";
                                mainList.Add(item);
                            }

                            lbOTGrid.ItemsSource = mainList;
                            this.Close();
                        }
                    }
                }
            }
            catch (Exception) { }
        }

        private void lstEmpName_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            try
            {
                department.Content = (((System.Windows.Controls.ListBox)(sender)).SelectedItems[0] as Employee).Department;
                designation.Content = (((System.Windows.Controls.ListBox)(sender)).SelectedItems[0] as Employee).Designation;
                //branch.Content = (((System.Windows.Controls.ListBox)(sender)).SelectedItems[0] as Employee).Branch;
            }
            catch (Exception) { }
        }

        private void lstEmpName_KeyUp(object sender, KeyEventArgs e)
        {
            try
            {
                //if (e.Key == Key.Enter)
                //    date.SetFocusOnDate();
                //else 
                if (e.Key == Key.Left)
                    lstBranch.Focus();
                else if (e.Key == Key.Right)
                    date.SetFocusOnDate();
            }
            catch (Exception) { }
        }

        private void IsHalfDay_KeyUp(object sender, KeyEventArgs e)
        {
            try
            {
                if (e.Key == Key.Enter)
                    IsHalfDay.IsChecked = !(IsHalfDay.IsChecked.Value);
            }
            catch (Exception) { }
        }

        private void lbOTGrid_KeyUp(object sender, KeyEventArgs e)
        {
            try
            {
                if (e.Key == Key.Enter)
                    lstEmpName.Focus();
            }
            catch (Exception) { }
        }

        private void lstBranch_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            lstEmpName.ItemsSource = Database.GetEmployeesByBranch(lstBranch.SelectedValue.ToString());
            lstEmpName.DisplayMemberPath = "EmpIDName";
            lstEmpName.SelectedIndex = 0;
        }

        private void lstBranch_KeyUp(object sender, KeyEventArgs e)
        {
            try
            {
                if (e.Key == Key.Right)
                    lstEmpName.Focus();
            }
            catch (Exception) { }
        }
    }
}
